<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="事件名称" prop="affairName">
        <el-input
          v-model="queryParams.affairName"
          placeholder="请输入事件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="相关人员" prop="affairPson">

<!--        <el-input-->
<!--          v-model="queryParams.affairPson"-->
<!--          placeholder="请输入相关人员"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.affairPson"  placeholder="请选相关人员">
          <el-option
            v-for="item in psonOptions"
            :key="item.psonId"
            :label="item.psonName"
            :value="item.psonId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="事件日期" prop="affairDate">
        <el-date-picker clearable
          v-model="queryParams.affairDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择事件日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="地址" prop="affairAddress">
        <el-input
          v-model="queryParams.affairAddress"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:affair:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:affair:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:affair:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:affair:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="affairList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="事件id" align="center" prop="affairId" />
      <el-table-column label="事件名称" align="center" prop="affairName" />
<!--      <el-table-column label="相关人员" align="center" prop="affairPson" />-->
      <el-table-column label="相关人员" align="center" prop="affairPsonName" />
      <el-table-column label="事件日期" align="center" prop="affairDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.affairDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center" prop="affairAddress" />
      <el-table-column label="照片url" align="center" prop="photoUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.photoUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>

      <el-table-column label="文件url" align="center" prop="fileUrl">
        <template slot-scope="scope">
          <a :href="'http://localhost:8080'+scope.row.fileUrl">
            <el-button type="success">下载文件</el-button>
          </a>
        </template>
      </el-table-column>

      <el-table-column label="详情" align="center" prop="affairInfo">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-show"
            @click="affairInfoShow(scope.row)"
          >查看详情
          </el-button>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:affair:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:affair:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改事件编号对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="事件名称" prop="affairName">
          <el-input v-model="form.affairName" placeholder="请输入事件名称" />
        </el-form-item>
        <el-form-item label="相关人员" prop="affairPson">
          <el-select v-model="form.affairPson" multiple placeholder="请选相关人员">
            <el-option
              v-for="item in psonOptions"
              :key="item.psonId"
              :label="item.psonName"
              :value="item.psonId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="事件日期" prop="affairDate">
          <el-date-picker clearable
            v-model="form.affairDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择事件日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="地址" prop="affairAddress">
          <el-input v-model="form.affairAddress" placeholder="请输入地址" />
        </el-form-item>

        <el-form-item label="照片url" prop="photoUrl">
          <image-upload v-model="form.photoUrl"/>
        </el-form-item>
        <el-form-item label="文件url" prop="fileUrl">
          <file-upload v-model="form.fileUrl"/>
        </el-form-item>

        <el-form-item label="事件详情">
          <editor v-model="form.affairInfo" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 详情 -->
    <el-dialog title="详情" :visible.sync="openAffairInfo" width="800px" append-to-body>
      <span v-html="setAffairInfo"></span>
      <div slot="footer" class="dialog-footer">
        <el-button @click="psonInfoCancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>




</template>

<script>
import { listAffair, getAffair, delAffair, addAffair, updateAffair } from "@/api/system/affair";
import {getAllPson} from "@/api/system/pson";

export default {
  name: "Affair",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      setAffairInfo: null,
      openAffairInfo: false,
      psonOptions: [],
      // 总条数
      total: 0,
      // 事件编号表格数据
      affairList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        affairName: null,
        affairPson: null,
        affairDate: null,
        affairAddress: null,
        affairInfo: null,
        photoUrl: null,
        fileUrl: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        affairName: [
          { required: true, message: "事件名称不能为空", trigger: "blur" }
        ]

      }
    };
  },
  created() {
    this.getList();
    this.getPsonList();
  },
  methods: {
    affairInfoShow(row) {
      this.setAffairInfo = row.affairInfo;
      this.openAffairInfo = true;

    },
    psonInfoCancel() {
      this.openAffairInfo = false;
      this.setAffairInfo = null;
    },
    getPsonList() {
      this.psonOptions = [];
      getAllPson().then(response => {
        this.psonOptions = response.data;
      });
    },

    /** 查询事件编号列表 */
    getList() {
      this.loading = true;
      listAffair(this.queryParams).then(response => {
        this.affairList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        affairId: null,
        affairName: null,
        affairPson: null,
        affairDate: null,
        affairAddress: null,
        affairInfo: null,
        photoUrl: null,
        fileUrl: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.affairId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加事件编号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const affairId = row.affairId || this.ids
      getAffair(affairId).then(response => {
        this.form = response.data;
        this.form.affairPson = this.form.affairPson.split(",").map(Number);
        this.open = true;
        this.title = "修改事件编号";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.affairPson = this.form.affairPson.join(",");
          if (this.form.affairId != null) {
            updateAffair(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAffair(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const affairIds = row.affairId || this.ids;
      this.$modal.confirm('是否确认删除事件编号编号为"' + affairIds + '"的数据项？').then(function() {
        return delAffair(affairIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/affair/export', {
        ...this.queryParams
      }, `affair_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
